package com.bed.lovewithpatientbed.controller;

import com.bed.lovewithpatientbed.common.bean.LayUiPageBean;
import com.bed.lovewithpatientbed.pojo.Employee;
import com.bed.lovewithpatientbed.pojo.Maintain;
import com.bed.lovewithpatientbed.pojo.Malfunction;
import com.bed.lovewithpatientbed.pojo.PageBean;
import com.bed.lovewithpatientbed.service.MaintainService;
import com.bed.lovewithpatientbed.service.MalfunctionService;
import com.bed.lovewithpatientbed.util.LayUiUtil;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author myp
 * @version 1.0.0
 * @ClassName AfterSalController.java
 * @Description 售后管理Controller
 */
@Controller
@RequestMapping("afterSal")
public class AfterSalController {

    @Resource
    private MalfunctionService malfunctionService;
    @Resource
    private MaintainService maintainService;

    /**
     * @title 新增故障记录
     * @description 
     * @author myp 
     * @updateTime   
     * @throws 
     */
    @RequestMapping("saveMalfunctionDO")
    @ResponseBody
    public Map<String,Object> saveMalfunctionDo(Malfunction malfunction){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Integer i = malfunctionService.saveMalfunction(malfunction);
            if (i > 0) {
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
    /**
     * @title 增加故障维修人员
     * @description 
     * @author myp 
     * @updateTime   
     * @throws 
     */
    @RequestMapping("repairDO")
    @ResponseBody
    public Map<String,Object> repair(Malfunction malfunction,Employee employee){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Integer i = malfunctionService.repairMalfunction(malfunction,employee);
            if (i > 0) {
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
    /**
     * @title 修改故障状态
     * @description 
     * @author myp 
     * @updateTime   
     * @throws 
     */
    @RequestMapping("updateMalfunction")
    @ResponseBody
    public Map<String,Object> updateMalfunction(Malfunction malfunction){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Integer i = malfunctionService.updateMalfunction(malfunction);
            if (i > 0) {
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }

    /**
     * @title 故障记录列表查询
     * @description 条件组合查询，分页查询
     * @author myp 
     * @updateTime   
     * @throws 
     */
    @RequestMapping("selectMalfunctionListDO")
    @ResponseBody
    public Map<String,Object> selectMalfunctionListDO(LayUiPageBean pageBean,Malfunction malfunction,@DateTimeFormat(pattern = "yyyy-MM-dd")Date staData , @DateTimeFormat(pattern = "yyyy-MM-dd")Date endDate){

        Map<String,Object> resultMap = null;
        try{
            List<Malfunction> malfunctionList = malfunctionService.selectMalfunctionList(pageBean,malfunction,staData,endDate);
            Integer count=malfunctionService.getCount(malfunction,staData,endDate);
            resultMap = LayUiUtil.getLayTableResponseMap("success", 0, count, malfunctionList);
        }catch (Exception e){
            e.printStackTrace();
            resultMap = LayUiUtil.getLayTableResponseMap("failed", 400, 0, null);
        }
        return resultMap;
    }
    /**
     * @title 故障记录查询
     * @description 根据故障记录编号查找记录
     * @author myp
     * @updateTime
     * @throws
     */
    @RequestMapping("selectMalfunctionDo")
    @ResponseBody
    public Map<String,Object> selectMalfunctionDo(Integer malfunctionId){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Malfunction malfunction = malfunctionService.selectMalfunction(malfunctionId);
            resultMap.put("malfunction",malfunction);
            resultMap.put("result",true);
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;

    }

    /**
     * @title 新增维修记录
     * @description
     * @author myp
     * @updateTime
     * @throws
     */
    @RequestMapping("saveMaintainDo")
    @ResponseBody
    public Map<String,Object> saveMaintainDo(Maintain maintain){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Integer i = maintainService.saveMaintain(maintain);
            if (i > 0) {
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
    /**
     * @title 维修记录列表查询
     * @description 条件组合查询，分页查询
     * @author myp
     * @updateTime
     * @throws
     */
    @RequestMapping("selectMaintainListDo")
    @ResponseBody
    public Map<String,Object> selectMaintainListDo(LayUiPageBean pageBean, Maintain maintain, @DateTimeFormat(pattern = "yyyy-MM-dd") Date staData , @DateTimeFormat(pattern = "yyyy-MM-dd")Date endDate){
        Map<String,Object> resultMap = null;
        try{
            List<Maintain> maintainList = maintainService.selectMaintainList(pageBean, maintain, staData, endDate);
            Integer count=maintainService.getCount(maintain,staData, endDate);
            resultMap = LayUiUtil.getLayTableResponseMap("success", 0, count, maintainList);
        }catch (Exception e){
            e.printStackTrace();
            resultMap = LayUiUtil.getLayTableResponseMap("failed", 400, 0, null);
        }
        return resultMap;
    }
    /**
     * @title 维修记录查询
     * @description 根据维修记录编号查找记录
     * @author myp
     * @updateTime
     * @throws
     */
    @RequestMapping("selectMaintainDO")
    @ResponseBody
    public Map<String,Object> selectMaintainDO(Integer maintainId){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Maintain maintain = maintainService.selectMaintain(maintainId);
            resultMap.put("maintain",maintain);
            resultMap.put("result",true);
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
    /**
     * @title 修改维修状态
     * @description 
     * @author myp 
     * @updateTime   
     * @throws 
     */
    @RequestMapping("updateMaintain")
    @ResponseBody
    public Map<String,Object> updateMaintain(Maintain maintain){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Integer i = maintainService.update(maintain);
            if (i > 0) {
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
}
